#include "StdAfx.h"
#include "AirlineServ.h"
#include "UtilService.h"
#include "Util.h"

bool AirlineServ::AirlineAuth(char szAccount[], char szPasswd[])
{
	MSG_AIRLINE_AUTH authMsg;
	strcpy(authMsg.szAccount, szAccount);
	strcpy(authMsg.szPasswd, szPasswd);

	SendMessage(&authMsg, m_sock);

	MSG_BASE* pMsg = NULL;
	while ((pMsg = m_splitter.ExtractMsg()) == NULL)
	{
		int nRecv = m_splitter.RecvData(m_sock);
		if (nRecv <= 0)
			throw CException("Connection terminated");
	}

	if (pMsg->nMsgType != MSGTYPE_SUCCESS_REPLY)
	{
		delete pMsg;
		throw CException("Message type error in CusServ::UserAuth");
	}

	MSG_SUCCESS_REPLY* pSucMsg = (MSG_SUCCESS_REPLY*)pMsg;
	bool bRet = pSucMsg->bSuccess;
	delete pSucMsg;

	return bRet;
}

void AirlineServ::ViewOrders(vector<ORDER_ENTRY>*& pVecRes)
{
	MSG_VIEW_ORDER_REQUEST viewMsg;
	SendMessage(&viewMsg, m_sock);

	MSG_BASE* pMsg = NULL;
	while ((pMsg = m_splitter.ExtractMsg()) == NULL)
	{
		int nRecv = m_splitter.RecvData(m_sock);
		if (nRecv <= 0)
			throw CException("Connection terminated");
	}

	if (pMsg->nMsgType != MSGTYPE_VIEW_ORDER_CONTENT)
	{
		delete pMsg;
		throw CException("Message type error in CusServ::ViewOrders");
	}

	pVecRes = new vector<ORDER_ENTRY>;
	MSG_VIEW_ORDER_CONTENT* pOrderMsg = (MSG_VIEW_ORDER_CONTENT*)pMsg;
	for (int i = 0; i < pOrderMsg->nEntryNum; i++)
		pVecRes->push_back(pOrderMsg->orders[i]);

	delete pOrderMsg;
}

bool AirlineServ::CancelOrder(int nOrderId)
{
	MSG_CANCEL_ORDER cancelMsg;
	cancelMsg.nReservId = nOrderId;

	SendMessage(&cancelMsg, m_sock);

	MSG_BASE* pMsg = NULL;
	while ((pMsg = m_splitter.ExtractMsg()) == NULL)
	{
		int nRecv = m_splitter.RecvData(m_sock);
		if (nRecv <= 0)
			throw CException("Connection terminated");
	}

	if (pMsg->nMsgType != MSGTYPE_SUCCESS_REPLY)
	{
		delete pMsg;
		throw CException("Message type error in CusServ::CancelOrder");
	}

	MSG_SUCCESS_REPLY* pSucMsg = (MSG_SUCCESS_REPLY*)pMsg;
	bool bRet = pSucMsg->bSuccess;
	delete pSucMsg;

	return bRet;
}

bool AirlineServ::AddRoute(ROUT_ENTRY& route)
{
	MSG_AIRLINE_ADDROUTE addMsg;
	addMsg.entry = route;

	SendMessage(&addMsg, m_sock);

	MSG_BASE* pMsg = NULL;
	while ((pMsg = m_splitter.ExtractMsg()) == NULL)
	{
		int nRecv = m_splitter.RecvData(m_sock);
		if (nRecv <= 0)
			throw CException("Connection terminated");
	}

	if (pMsg->nMsgType != MSGTYPE_SUCCESS_REPLY)
	{
		delete pMsg;
		throw CException("Message type error in CusServ::CancelOrder");
	}

	MSG_SUCCESS_REPLY* pSucMsg = (MSG_SUCCESS_REPLY*)pMsg;
	bool bRet = pSucMsg->bSuccess;
	delete pSucMsg;

	return bRet;
}

void AirlineServ::ViewRoutes(vector<ROUT_ENTRY>*& pVecRes)
{
	MSG_VIEW_ROUTES msg;
	SendMessage(&msg, m_sock);

	MSG_BASE* pMsg = NULL;
	while ((pMsg = m_splitter.ExtractMsg()) == NULL)
	{
		int nRecv = m_splitter.RecvData(m_sock);
		if (nRecv <= 0)
			throw CException("Connection terminated");
	}

	if (pMsg->nMsgType != MSGTYPE_ROUTE_REPLY)
	{
		delete pMsg;
		throw CException("Message type error in CusServ::CancelOrder");
	}

	pVecRes = new vector<ROUT_ENTRY>;
	if (pVecRes == NULL)
		return;

	MSG_ROUTE_REPLY* pReplyMsg = (MSG_ROUTE_REPLY*)pMsg;
	for (int i = 0; i < pReplyMsg->nEntryNum; i++)
		pVecRes->push_back(pReplyMsg->entries[i]);
	delete pReplyMsg;
}